package de.karlNet.sshTray.gui;

import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import de.karlNet.sshTray.controller.SettingsController;
import de.karlNet.sshTray.model.Settings;

import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

@Component
public class SettingsWindow extends JFrame{
	@Autowired
	private SettingsController settingsController;
	private JTextField txtTerminalpath;
	public SettingsWindow() {
		getContentPane().setLayout(new BorderLayout(0, 0));
		setTitle("Manage Settings");
		setSize(600, 100);
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.SOUTH);
		panel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
		
		JButton btnOk = new JButton("OK");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
				settingsController.updateSettings(new Settings().setTerminalPath(txtTerminalpath.getText()));
			}
		});
		panel.add(btnOk);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
			}
		});
		panel.add(btnCancel);
		
		JPanel panel_1 = new JPanel();
		getContentPane().add(panel_1, BorderLayout.CENTER);
		GridBagLayout gbl_panel_1 = new GridBagLayout();
		gbl_panel_1.columnWidths = new int[]{0, 0, 0};
		gbl_panel_1.rowHeights = new int[]{0, 0};
		gbl_panel_1.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		gbl_panel_1.rowWeights = new double[]{0.0, Double.MIN_VALUE};
		panel_1.setLayout(gbl_panel_1);
		
		JLabel lblTerminalPath = new JLabel("Terminal Path");
		GridBagConstraints gbc_lblTerminalPath = new GridBagConstraints();
		gbc_lblTerminalPath.insets = new Insets(0, 0, 0, 5);
		gbc_lblTerminalPath.anchor = GridBagConstraints.EAST;
		gbc_lblTerminalPath.gridx = 0;
		gbc_lblTerminalPath.gridy = 0;
		panel_1.add(lblTerminalPath, gbc_lblTerminalPath);
		
		txtTerminalpath = new JTextField();
		txtTerminalpath.setText("terminalPath");
		GridBagConstraints gbc_txtTerminalpath = new GridBagConstraints();
		gbc_txtTerminalpath.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtTerminalpath.gridx = 1;
		gbc_txtTerminalpath.gridy = 0;
		panel_1.add(txtTerminalpath, gbc_txtTerminalpath);
		txtTerminalpath.setColumns(10);
	}

	@Override
	public void setVisible(boolean vis) {
		super.setVisible(vis);
		if(vis) {
			this.txtTerminalpath.setText(this.settingsController.loadSettings().getTerminalPath());
		}
	}
}
